﻿package com.telecom.tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Connection;

import com.telecom.cms.util.Constant;
import com.telecom.cms.dao.GetConnect;
import com.telecom.cms.dto.*;
import com.telecom.cms.util.Initmap;

/**
 * @author 
 * 
 */
public class TreeUtil {
	
	public static List retrieveBranch_Name(String latnid) {
		List coll = new ArrayList();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		int latn_id = Integer.parseInt(latnid);
		
		HashMap atommap = Initmap.getatomMap();
		ArrayList mea = (ArrayList) atommap.get("地理位置细到支局");
		try {

			if (latn_id == 1000) {
				conn = GetConnect.getConnection();

				ps = conn.prepareStatement("select * from " + Constant.LOC_LATN
						+ "     order by Latn_Cd  ");

				rs = ps.executeQuery();
				while (rs.next()) {

					TreeInfoDTO info = new TreeInfoDTO();
					info.setId(rs.getString(1));
					info.setParentid("-100");
					info.setNodename(rs.getString(2));
					coll.add(info);

				}// 取本地网头节点

			}

			else if (latn_id == 1001) {

				// conn = GetConnect.getConnection();
				//
				// ps = conn.prepareStatement("select * from "
				// + Constant.DMN_BUREAU + " where Sub_Bureau_Id = "
				// + 1001 + " order by 1 ");
				//
				// rs = ps.executeQuery();
				// if (rs != null && rs.next()) {
				//
				// TreeInfoDTO info = new TreeInfoDTO();
				// info.setId(rs.getString(1));
				// info.setParentid("-100");
				// info.setNodename(rs.getString(3));
				// coll.add(info);
				//
				// }// 取本地网头节点

				TreeInfoDTO info = new TreeInfoDTO();
				info.setId("1001");
				info.setParentid("-100");
				info.setNodename("武汉");
				coll.add(info);

				List cachlist = new ArrayList(); // 定义List存放区局ID
				for (int i = 0; i < mea.size(); i++) {

					MEADTO listdto = (MEADTO) mea.get(i);

					if (listdto.getSTART_VAULE().equals("1001")) {
						TreeInfoDTO cash = new TreeInfoDTO();
						cash.setId(listdto.getITEM_ID());
						cash.setParentid("1001");
						cash.setNodename(listdto.getEND_VALUE());
						coll.add(cash);
						cachlist.add(listdto.getITEM_ID());

					}

				}

				List fenjulist = new ArrayList(); // 定义List存放分局ID

				for (int i = 0; i < cachlist.size(); i++) {

					String qjid = (String) cachlist.get(i);

					for (int j = 0; j < mea.size(); j++) {

						MEADTO listdto = (MEADTO) mea.get(j);

						if (listdto.getSTART_VAULE().equals(qjid)) {
							TreeInfoDTO cash = new TreeInfoDTO();
							cash.setId(listdto.getITEM_ID());
							cash.setParentid(qjid);
							cash.setNodename(listdto.getEND_VALUE());
							coll.add(cash);
							fenjulist.add(listdto.getITEM_ID());

						}

					}

				}

				for (int k = 0; k < fenjulist.size(); k++) { // 取支局

					String qjid = (String) fenjulist.get(k);

					for (int j = 0; j < mea.size(); j++) {

						MEADTO listdto = (MEADTO) mea.get(j);

						if (listdto.getSTART_VAULE().equals(qjid)) {
							TreeInfoDTO cash = new TreeInfoDTO();
							cash.setId(listdto.getITEM_ID());
							cash.setParentid(qjid);
							cash.setNodename(listdto.getEND_VALUE());
							coll.add(cash);

						}

					}

				}

				// List cachlist = new ArrayList(); // 定义List存放分局ID
				//
				// ps = conn.prepareStatement("select * from "
				// + Constant.DMN_BUREAU + " where Par_Branch_Id = "
				// + 1001 + " order by 1 ");
				//
				// rs = ps.executeQuery();
				// while (rs != null && rs.next()) {
				//
				// TreeInfoDTO info = new TreeInfoDTO();
				// info.setId(rs.getString(1));
				// info.setParentid(rs.getString(2));
				// info.setNodename(rs.getString(3));
				// coll.add(info);
				// cachlist.add(rs.getString(1));
				//
				// } // 取分区记录放入List数组

				// List subcashlist = new ArrayList(); // 定义List存放支局ID
				//
				// for (int i = 0; i < cachlist.size(); i++) {
				//
				// String branchid = cachlist.get(i).toString();
				//
				// ps = conn.prepareStatement("select * from "
				// + Constant.DMN_BUREAU + " where Par_Branch_Id = "
				// + branchid + " order by 1 ");
				//
				// rs = ps.executeQuery();
				// while (rs != null && rs.next()) {
				//
				// TreeInfoDTO info = new TreeInfoDTO();
				// info.setId(rs.getString(1));
				// info.setParentid(rs.getString(2));
				// info.setNodename(rs.getString(3));
				// coll.add(info);
				// subcashlist.add(rs.getString(1));
				//
				// }
				//
				// } // 遍历分区ID去相应支局放入树状结构List

				// for (int i = 0; i < subcashlist.size(); i++) {
				//
				// String branchid = subcashlist.get(i).toString();
				//
				// ps = conn.prepareStatement("select * from "
				// + Constant.DMN_BUREAU + " where Par_Branch_Id = "
				// + branchid + " order by 1 ");
				//
				// rs = ps.executeQuery();
				// while (rs != null && rs.next()) {
				//
				// TreeInfoDTO info = new TreeInfoDTO();
				//						info.setId(rs.getString(1));
				//						info.setParentid(rs.getString(2));
				//						info.setNodename(rs.getString(3));
				//						coll.add(info);
				//					}
				//
				//				}
			}

			else {

				for (int j = 0; j < mea.size(); j++) {

					MEADTO listdto = (MEADTO) mea.get(j);

					if (listdto.getITEM_ID().equals(latnid)) {
						TreeInfoDTO cash = new TreeInfoDTO();
						cash.setId(listdto.getITEM_ID());
						cash.setParentid("-100");
						cash.setNodename(listdto.getEND_VALUE());
						coll.add(cash);
						break;

					}

				}// 取本地网头节点

				List cachlist = new ArrayList(); // 定义List存放分局ID

				for (int j = 0; j < mea.size(); j++) {

					MEADTO listdto = (MEADTO) mea.get(j);

					if (listdto.getSTART_VAULE().equals(latnid)) {
						TreeInfoDTO cash = new TreeInfoDTO();
						cash.setId(listdto.getITEM_ID());
						cash.setParentid(latnid);
						cash.setNodename(listdto.getEND_VALUE());
						coll.add(cash);
						cachlist.add(listdto.getITEM_ID());
					}

				}// 存放分局

				for (int i = 0; i < cachlist.size(); i++) {

					String qjid = (String) cachlist.get(i);

					for (int j = 0; j < mea.size(); j++) {

						MEADTO listdto = (MEADTO) mea.get(j);

						if (listdto.getSTART_VAULE().equals(qjid)) {
							TreeInfoDTO cash = new TreeInfoDTO();
							cash.setId(listdto.getITEM_ID());
							cash.setParentid(qjid);
							cash.setNodename(listdto.getEND_VALUE());
							coll.add(cash);

						}

					}

				}

				// conn = GetConnect.getConnection();
				//
				// ps = conn.prepareStatement("select * from "
				// + Constant.DMN_BUREAU + " where Sub_Bureau_Id = "
				// + latn_id + " order by 1 ");
				//
				// rs = ps.executeQuery();
				// if (rs != null && rs.next()) {
				//
				// TreeInfoDTO info = new TreeInfoDTO();
				// info.setId(rs.getString(1));
				// info.setParentid("-100");
				// info.setNodename(rs.getString(3));
				// coll.add(info);
				//
				// }// 取本地网头节点

				// List cachlist = new ArrayList(); // 定义List存放分局ID
				//
				// ps = conn.prepareStatement("select * from "
				// + Constant.DMN_BUREAU + " where Par_Branch_Id = "
				// + latn_id + " order by 1 ");
				//
				// rs = ps.executeQuery();
				// while (rs != null && rs.next()) {
				//
				// TreeInfoDTO info = new TreeInfoDTO();
				// info.setId(rs.getString(1));
				// info.setParentid(rs.getString(2));
				// info.setNodename(rs.getString(3));
				// coll.add(info);
				// cachlist.add(rs.getString(1));
				//
				// } // 取分区记录放入List数组
				// for (int i = 0; i < cachlist.size(); i++) {
				//
				// String branchid = cachlist.get(i).toString();
				//
				// ps = conn.prepareStatement("select * from "
				// + Constant.DMN_BUREAU + " where Par_Branch_Id = "
				// + branchid + " order by 1 ");
				//
				// rs = ps.executeQuery();
				// while (rs != null && rs.next()) {
				//
				// TreeInfoDTO info = new TreeInfoDTO();
				// info.setId(rs.getString(1));
				// info.setParentid(rs.getString(2));
				//						info.setNodename(rs.getString(3));
				//						coll.add(info);
				//
				//					}
				//
				//				} // 遍历分区ID去相应支局放入树状结构List
			}

		}

		catch (Exception e) {

			e.printStackTrace();

		}

		return coll;

	}

	public static List retrieveNodeInfos(String tablename) {
		List coll = new ArrayList();
		Connection conn = null;

		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			conn = GetConnect.getConnection();

			String sql = null;
			sql = "select * from  " + tablename + " order by 1";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();

			while (rs != null && rs.next()) {
				TreeInfoDTO info = new TreeInfoDTO();
				info.setId(rs.getString(1));
				info.setParentid(rs.getString(2));
				info.setNodename(rs.getString(3));
				coll.add(info);
			}
			if (rs != null) {
				rs.close();
			}
			if (ps != null) {
				ps.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return coll;
	}

	public static List retrieveNodeInfos(String tablename, String desctype) {
		List coll = new ArrayList();
		Connection conn = null;

		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			conn = GetConnect.getConnection();

			String sql = null;
			sql = "select * from  " + tablename + " where Share_Kind = '"
					+ desctype + "' order by 2,3";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();

			while (rs != null && rs.next()) {
				TreeInfoDTO info = new TreeInfoDTO();
				info.setId(rs.getString(1));
				info.setParentid(rs.getString(2));
				info.setNodename(rs.getString(3));
				coll.add(info);
			}
			if (rs != null) {
				rs.close();
			}
			if (ps != null) {
				ps.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return coll;
	}

	//	public static List retrieveNodeInfos(String tablename, String atomname) {
	//		List coll = new ArrayList();
	//		Connection conn = null;
	//
	//		PreparedStatement ps = null;
	//		ResultSet rs = null;
	//
	//		try {
	//			conn = GetConnect.getConnection();
	//			String sql = null;
	//			if (tablename.indexOf("SEG_USER_FOLDER_DEF") > -1) {
	//				sql = "select * from  " + tablename + " order by 2,3";
	//
	//				ps = conn.prepareStatement(sql);
	//				rs = ps.executeQuery();
	//
	//				while (rs != null && rs.next()) {
	//					TreeInfoDTO info = new TreeInfoDTO();
	//					info.setId(rs.getString(1));
	//					info.setParentid(rs.getString(2));
	//					info.setNodename(rs.getString(3));
	//					coll.add(info);
	//				}
	//			} else {
	//				HashMap atommap = Initmap.getatomMap();
	//
	//				List mea = (ArrayList) atommap.get(atomname);
	//
	//				for (int m = 0; m < mea.size(); m++) {
	//
	//					MEADTO listdto = (MEADTO) mea.get(m);
	//					TreeInfoDTO info = new TreeInfoDTO();
	//					info.setId(listdto.getITEM_ID());
	//					info.setParentid(listdto.getSTART_VAULE());
	//					info.setNodename(listdto.getEND_VALUE());
	//					coll.add(info);
	//
	//				}
	//
	//			}
	//
	//			// if(rs!=null){
	//			// rs.close();
	//			// rs=null;
	//			// }
	//			// if(ps!=null){
	//			// ps.close();
	//			// ps=null;
	//			// }
	//		} catch (Exception e) {
	//			System.out.println(e);
	//		}
	//		return coll;
	//	}

	/**
	 * @param alist
	 * @param nodename
	 * @return
	 */
	public static String createTreeInfo(List checkedAtom, List alist,
			String nodename) {
		StringBuffer contents = new StringBuffer();
		String checkedvalue = null;
		if (null != checkedAtom) {
			for (int myi = 0; myi < checkedAtom.size(); myi++) {
				ShowatomDTO sAtom = (ShowatomDTO) checkedAtom.get(myi);
				if (null != sAtom && sAtom.getAtomname().equals(nodename)) {
					checkedvalue = sAtom.getChinesevalue();
					break;
				}
			}
		}
		contents.append("var Tree" + nodename + " = new dTree('Tree" + nodename
				+ "');");// create
		// a
		// array
		// in
		contents.append("\n");

		// javascript

		for (int max = alist.size(), i = 0; i < max; i++) {
			TreeInfoDTO info = (TreeInfoDTO) alist.get(i);

			String check = "";
			if (checkedvalue != null
					&& checkedvalue.indexOf(info.getNodename() + ",") > -1) {
				check = "checked";
			}
			contents.append("Tree" + nodename + ".add(" + "\"");
			contents.append(info.getId());
			contents.append("\",");
			contents.append("\"");
			contents.append(info.getParentid());
			contents.append("\",");
			contents.append("\"");
			contents.append("<input type='checkbox' name='" + nodename
					+ "' id='" + "Tree" + nodename + i + "' value='"
					+ info.getId() + "' " + check
					+ " onclick='changeChild(Tree" + nodename + "," + "Tree"
					+ nodename + i + ");" + "changeParent(Tree" + nodename
					+ "," + "Tree" + nodename + i + ");checkItemtree();'>"
					+ info.getNodename() + "\");");
			contents.append("\n");

		}

		contents.append("document.write(Tree" + nodename + ");");
		contents.append("\n");
		return contents.toString();
	}

	/**
	 * @param alist
	 * @param nodename
	 * @return
	 */
	public static String createLeftTreeInfo(List alist, String nodename) {
		StringBuffer contents = new StringBuffer();

		contents.append("userAclModiTree = new dTree('userAclModiTree');");// create
		// a
		// array
		// in
		contents.append("\n");

		// javascript

		for (int max = alist.size(), i = 0; i < max; i++) {

			TreeInfoDTO info = (TreeInfoDTO) alist.get(i);
			contents.append("userAclModiTree.add(" + "\"");
			contents.append(info.getId());
			contents.append("\",");
			contents.append("\"");
			contents.append(info.getParentid());
			contents.append("\",");
			contents.append("\"");
			contents.append(info.getNodename());
			contents.append("\",");
			contents.append("\"");
			contents.append("../topreview.do?id=" + info.getId());
			contents.append("\",");
			contents.append("\"");
			contents.append("\",\"upFrame\"");
			contents.append(");");
			contents.append("\n");

		}
		contents.append("document.write(userAclModiTree);");

		contents.append("\n");
		return contents.toString();
	}

	/**
	 * @param alist
	 * @param nodename
	 * @return
	 */
	public static String createBoxTreeInfo(List alist, String nodename) {
		StringBuffer contents = new StringBuffer();
		Connection conn = null;

		PreparedStatement ps = null;
		ResultSet rs = null;
		contents.append("userAclModiTree = new dTree('userAclModiTree');");// create
		// a
		// array
		// in
		contents.append("\n");

		// javascript

		for (int max = alist.size(), i = 0; i < max; i++) {

			TreeInfoDTO info = (TreeInfoDTO) alist.get(i);
			String treeInfoId = info.getId();
			contents.append("userAclModiTree.add(" + "\"");
			contents.append(treeInfoId);
			contents.append("\",");
			contents.append("\"");
			contents.append(info.getParentid());
			contents.append("\",");
			contents.append("\"");

			try {

				conn = GetConnect.getConnection();

				ps = conn.prepareStatement("select * from  "
						+ Constant.SEG_APP_SUBJECT_DEF + " where FOLEDER_ID = "
						+ treeInfoId);
				rs = ps.executeQuery();
				if (rs.next()) {
					contents.append(info.getNodename() + "\");");
				} else {
					contents.append("<input type='checkbox' name='" + nodename
							+ "' id='" + i + "' value='" + info.getId()
							+ "' onclick='change(this)'>" + info.getNodename()
							+ "\");");

				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			contents.append("\n");

		}
		contents.append("document.write(userAclModiTree);");

		contents.append("\n");

		return contents.toString();
	}
	
	public static List getIdaFolderNode() {
		List coll = new ArrayList();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = GetConnect.getConnection();
			String sql = "SELECT * FROM " + Constant.SEG_USER_FOLDER_DEF + " WHERE Share_Kind='5' order by 2,3";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs != null && rs.next()) {
				TreeInfoDTO info = new TreeInfoDTO();
				info.setId(rs.getString(1));
				info.setParentid(rs.getString(2));
				info.setNodename(rs.getString(3));
				coll.add(info);
			}
			if (rs != null) 
				rs.close();
			if (ps != null) 
				ps.close();
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
		return coll;
	}
	
	public static String createIdaFolder(String ctrlName,String ctrlValue) {
		List nlist = getIdaFolderNode();
		StringBuffer contents = new StringBuffer();
		contents.append("var idaFolderTree = new dTree('idaFolderTree');");// create
		contents.append("\n");
		for (int i = 0; i < nlist.size(); i++) {
			TreeInfoDTO info = (TreeInfoDTO)nlist.get(i);
			String treeInfoId = info.getId();
			contents.append("idaFolderTree.add(" + "\"");
			contents.append(treeInfoId);
			contents.append("\",");
			contents.append("\"");
			contents.append(info.getParentid());
			contents.append("\",");
			contents.append("\"");
			contents.append("<input type='checkbox' name='" + ctrlName + "' " +
					"value='" + info.getId() + "' onclick='changeTree(this)' " +
					(ctrlValue.equals(info.getId()) ? "checked" : "") +
					">" + info.getNodename() + "\");");
			contents.append("\n");
		}
		contents.append("document.write(idaFolderTree);");
		contents.append("\n");
		return contents.toString();
	}
	
	public static String createIdaMenuTree(List tmptList) {
		List nlist = getIdaFolderNode();
		StringBuffer contents = new StringBuffer();
		contents.append("idaMenuTree = new dTree('idaMenuTree');");// create
		contents.append("\n");
		for (int i = 0; i < nlist.size(); i++) {
			TreeInfoDTO info = (TreeInfoDTO) nlist.get(i);
			contents.append("idaMenuTree.add(" + "\"");
			contents.append(info.getId());
			contents.append("\",");
			contents.append("\"");
			contents.append(info.getParentid());
			contents.append("\",");
			contents.append("\"");
			contents.append(info.getNodename());
			contents.append("\",");
			contents.append("\"");
			contents.append("");
			contents.append("\",");
			contents.append("\"");
			contents.append("\",\"upFrame\"");
			contents.append(");");
			contents.append("\n");
		}
		for (int i = 0; i < tmptList.size(); i++) {
			IdaTemplate template = (IdaTemplate)tmptList.get(i);
			if (template.getTemplateTypeId().equals("20"))
				continue;
			contents.append("idaMenuTree.add(" + "\"");
			contents.append(template.getTemplateId());
			contents.append("\",");
			contents.append("\"");
			contents.append(template.getFolderId());
			contents.append("\",");
			contents.append("\"");
			contents.append(template.getTemplateName());
			contents.append("\",");
			contents.append("\"");
			contents.append("ida.do?method=runIda&templateId=" + template.getTemplateId());
			contents.append("\",");
			contents.append("\"");
			contents.append("\",\"upFrame\"");
			contents.append(");");
			contents.append("\n");
		}
		contents.append("document.write(idaMenuTree);");
		contents.append("\n");
		return contents.toString();
	}
	
	public static String createIdaInstanceTree(List instList,String userId) { 
		StringBuffer contents = new StringBuffer();
		contents.append("idaInstanceTree = new dTree('idaInstanceTree');");// create
		contents.append("\n");
		contents.append("idaInstanceTree.add('-1','-100','我的取数','','','upFrame');\n");
		contents.append("idaInstanceTree.add('-2','-1','取数实例','','','upFrame');\n");
		for (int i = 0; i < instList.size(); i++) {
			IdaTemplateInstance instance = (IdaTemplateInstance)instList.get(i);
			if (!instance.getUserId().equals(userId))
				continue;
			contents.append("idaInstanceTree.add(" + "\"");
			contents.append(instance.getInstanceId());
			contents.append("\",");
			contents.append("\"");
			contents.append("-2");
			contents.append("\",");
			contents.append("\"");
			contents.append(instance.getInstanceName());
			contents.append("\",");
			contents.append("\"");
			contents.append("ida.do?method=runInstance&instanceId=" + instance.getInstanceId());
			contents.append("\",");
			contents.append("\"");
			contents.append("\",\"upFrame\"");
			contents.append(");");
			contents.append("\n");
		}
		contents.append("document.write(idaInstanceTree);");
		contents.append("\n");
		return contents.toString();
	}

	public static void main(String[] args) {
		// List alist =
		// TreeUtil.retrieveNodeInfos(Constant.SEG_USER_FOLDER_DEF);
		// TreeInfo info = null;
		// for(Iterator i = c.iterator();i.hasNext();){
		// info = (TreeInfo)i.next();
		// System.out.println("*****" + info.getNodeName());
		// }

		List alist = TreeUtil.retrieveBranch_Name("1006");
		System.out.println(TreeUtil.createBoxTreeInfo(alist, "地理位置(支局)"));

		// for (int i = 0; i < alist.size(); i++) {
		// TreeInfoDTO info = (TreeInfoDTO) alist.get(i);
		// System.out.println(info.getId());
		// }
	}
}
